package com.cosina.game.colorball;

import java.util.List;

import android.graphics.Bitmap;

public class Ball {
	
	public static Ball DEAD_BALL = new Ball(-1,-1,-1,null);
	static {
		DEAD_BALL.health = false;
	};
	
	
    public boolean health = true;
	public void update(int x, int y){
		this.x = x;
		this.y = y;
	}
    private int x, y;
    private final int color;
    private final BallGame parent;
    
    public Ball(int x,int y,int color,BallGame parent){
    	this.x = x;
    	this.y = y;
    	this.parent = parent;
    	this.color = color;
    }

    public Bitmap getDrawingPic(){
    	if(health)
    		return BallView.pics[color];
    	return null;
    }
    
    public void sick(List<Ball> ambulance){
    	if(health){
    		ambulance.add(this);
    		this.health = false;
    		infect(ambulance);
    	}
    }
    
    public void cure() {
    	health = true;
	}
    
    private boolean canInfect(int otherColor){
    	return this != Ball.DEAD_BALL && this.color == otherColor;
    }

    public void infect(List<Ball> ambulance){
    	{
    		Ball upBall = getUp();
    		if(upBall.canInfect(this.color))
    			upBall.sick(ambulance);
    	}
    	{
    		Ball downBall = getDown();
    		if(downBall.canInfect(this.color))
    			downBall.sick(ambulance);
    	}
    	{
    		Ball leftBall = getLeft();
    		if(leftBall.canInfect(this.color))
    			leftBall.sick(ambulance);
    	}
    	{
    		Ball rightBall = getRight();
    		if(rightBall.canInfect(this.color))
    			rightBall.sick(ambulance);
    	}
    }
    
    private Ball getUp(){
    	return parent.getBall(this.x, this.y-1);
    }
    private Ball getDown(){
    	return parent.getBall(this.x, this.y+1);
    }
    private Ball getLeft(){
    	return parent.getBall(this.x-1, this.y);
    }
    private Ball getRight(){
    	return parent.getBall(this.x+1, this.y);
    }

	
}
